package cn.biplam.back.operator.ddl.sql2md;

import java.util.ArrayList;

import cn.biplam.back.sql.dml.Insert;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.interfaces.DataOperator;
import cn.biplam.common.opcode.sqlengine.DDLParserCode.FkTabBufCode;
import cn.biplam.common.struc.MdStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 * 源数据表外键关系缓冲区数据操作类
 * <li>插入表外键关系到缓存区 
 * @author XiaoRuxing 2009-6-26下午09:11:30
 * @version 1.0
 */
public class FkTableBuffer extends DataOperator{
	
	public FkTableBuffer() throws ErrorMessage{
		super("table_fk");
	}
	
	@Override
	protected OperatorResult _exchange() throws ErrorMessage {
		ArrayList<String> sql = new ArrayList<String>();
		  if(this.opId.equals(FkTabBufCode.Insert)){
			this.insert(sql, filter);
			return this.doing(sql, Operator_Type.DML);
		}else if(this.opId.equals(FkTabBufCode.Get_By_ID)){
			this.selectById(sql, filter);
			return this.doing(sql, Operator_Type.Select);
		}else if(this.opId.equals(FkTabBufCode.Get_By_TaskID_And_MtIdOrFtId)){
			this.selectByTaskIdAndMtidOrFtid(sql, filter);
			return this.doing(sql, Operator_Type.Select);
		}else{
			throw new ErrorMessage("Data Operator id not found:"+ opId+" !");
		}
	}
	
	/** 插入表外键关系到缓存区 */
	public void insert(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		String mainTableId = filter.getValue(MdStruc.FkBufferStruc.Main_Table_ID);
		String foeignTableId = filter.getValue(MdStruc.FkBufferStruc.Foreign_Table_ID);
		String reid=generatorRowID();
		Insert insert = new Insert(this.tableName);
		insert.putStringFieldDenyNull(MdStruc.FkBufferStruc.Relation_ID, reid);
		insert.putStringFieldIgnoreNull(MdStruc.FkBufferStruc.Relation_Name, filter.getValue(MdStruc.FkBufferStruc.Relation_Name));
		insert.putStringFieldDenyNull(MdStruc.FkBufferStruc.Main_Table_ID, mainTableId);
		insert.putStringFieldDenyNull(MdStruc.FkBufferStruc.Foreign_Table_ID, foeignTableId);
		insert.putStringFieldIgnoreNull(MdStruc.FkBufferStruc.BATCH, filter.getValue(MdStruc.FkBufferStruc.BATCH));
		sql.add(insert.getSimpleSql());
	}
	
	/**
	 * 根据关系ID查询关系 
	 * @throws ErrorMessage 
	 */
	public void selectById(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		StringBuilder sp=new StringBuilder();
		sp.append("select * ");        
		sp.append("from "+this.tableName+"  t     ");
		sp.append("  where reid='");
		sp.append(filter.getValue(MdStruc.FkBufferStruc.Relation_ID));
		sp.append("'");
		sql.add(sp.toString());
	}
	
	/**
	 * 根据关系ID查询关系 
	 * @throws ErrorMessage 
	 */
	private void selectByTaskIdAndMtidOrFtid(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		StringBuilder sp=new StringBuilder();
		sp.append("select * ");        
		sp.append("from "+this.tableName+"  t     ");
		sp.append("  where batch='");
		sp.append(filter.getValue(MdStruc.FkBufferStruc.BATCH));
		sp.append("'");
		sp.append(" and(");
		sp.append( MdStruc.FkBufferStruc.Main_Table_ID);
		sp.append(" = '");
		sp.append(filter.getValue(MdStruc.FkBufferStruc.Main_Table_ID));
		sp.append("' or ");
		sp.append( MdStruc.FkBufferStruc.Foreign_Table_ID);
		sp.append(" = '");
		sp.append(filter.getValue(MdStruc.FkBufferStruc.Foreign_Table_ID));
		sp.append("')");
		sql.add(sp.toString());
	}
	
	/**
	 * 根据模型类型初始化表名  
	 */
	private void _init(DataFilter filter) throws ErrorMessage{
		String modelType = filter.getValue(MdStruc.FkBufferStruc.MODEL_TYPE);
		if(TOOLS.StringIsEmpty(modelType)){
			throw new ErrorMessage("Model type is null");
		}
		this.tableName = "src_table_fk";
	}
}
